package com.hangxintong.common;

import com.hangxintong.common.utils.RedisUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.core.io.ClassPathResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.mail.MessagingException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.io.UnsupportedEncodingException;
import java.util.Random;

@Component
public class MailSenderUtils {
    @Resource
    JavaMailSenderImpl mailSender;
    @Resource
    RedisUtil redisUtil;

    private static final String sendFrom = "customer@hangxintong.com";
    private static final String sendFromName = "行信通客服";
    public MailSenderUtils() {
    }

    //简单发送邮件
    public void sendSimpleMail(String toEmail,String subTitle,String content) throws MessagingException, UnsupportedEncodingException {
        SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
        simpleMailMessage.setFrom(new InternetAddress(sendFrom,sendFromName, "UTF-8").toString());
        simpleMailMessage.setTo(toEmail);
        simpleMailMessage.setSubject(subTitle);
        simpleMailMessage.setText(content);
        mailSender.send(simpleMailMessage);
    }
    //复杂发送邮件
    public  void sendMimeMail(String toEmail,String subTitle,String content,String filePath) throws MessagingException, UnsupportedEncodingException {
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage);
        messageHelper.setFrom(new InternetAddress(sendFrom,sendFromName, "UTF-8").toString());
        messageHelper.setTo(toEmail);
        messageHelper.setSubject(subTitle);
        messageHelper.setText(content);
        if (!StringUtils.isBlank(filePath)) {
            messageHelper.addAttachment("work.docx", new File(filePath));
        }
        mailSender.send(mimeMessage);
    }

    /**
     * @Function: 生成验证码
     * @author: Mr.Bz
     * @Date:     2019/12/11 15:30
     */
    public static String getMsgCode() {
        int n = 8;
        StringBuilder code = new StringBuilder();
        Random ran = new Random();
        for (int i = 0; i < n; i++) {
            code.append(Integer.valueOf(ran.nextInt(10)).toString());
        }
        return code.toString();
    }


    /**
     * tsj
     * 发送带有html格式的邮件
     * @param toEmail 收件邮箱
     * @param subJect 主题
     * @param content  内容
     * @param flag     是否为html格式发送 true ->html flase-> 纯文本格式
     * @throws MessagingException
     */
    public void sendHtmlEmail(String toEmail,String subJect,String content,Boolean flag,String path) throws MessagingException, UnsupportedEncodingException {
        //1、创建文件发送器
        //设置邮件发送器  包括发送人 密码   服务器地址
//        JavaMailSenderImpl mailSender = new JavaMailSenderImpl();


        //使用JavaMail的MimeMessage，支持更加复杂的邮件格式和内容
        MimeMessage msg = mailSender.createMimeMessage();
        //创建MimeMessageHelper对象，处理MimeMessage的辅助类
        // msg:发送的邮件信息，true:是否为HTML格式的文件，utf-8:设置编码格式(因为发送html格式时、内容可能出现乱码)
        MimeMessageHelper helper = new MimeMessageHelper(msg, true,"utf-8");
        //使用辅助类MimeMessage设定参数
        helper.setFrom(new InternetAddress(sendFrom,sendFromName, "UTF-8").toString());
        helper.setTo(toEmail);
        helper.setSubject(subJect);
        helper.setText(content,flag);  //true参数说明该内容格式为HTML
        //加载文件资源，作为附件  路劲非空则添加文件
        if (!StringUtils.isBlank(path)) {
            ClassPathResource file = new ClassPathResource(path);
            //加入附件
            helper.addAttachment("default", file);  //键值对
        }
        // 发送邮件
        mailSender.send(msg);

    }
}
